package com.usermanage.controller;

import com.alibaba.fastjson.JSONObject;
import com.change.Service.AirSoftService;
import com.ordercore.Common.OrderIOC;
import com.ordercore.Util.ResponseData.ResponseData;
import com.pinza.Bean.NonStandardLibraryBean;
import com.pinza.Common.NonStandardLibraryIOC;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.*;
import com.usermanage.beanParam.*;
import com.usermanage.util.*;
import lombok.experimental.var;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/pmOrder")
public class PMOrderController {


    @Resource
    private PMOrderService pmOrderService;
    @Resource
    private ContractService contractService;

    @Resource
    private ContractRealTimePriceService contractRealTimePriceService;
    @Resource
    private CompanyService companyService;
    @Resource
    private MessageService messageService;
    @Resource
    private UserService userService;

    @RequestMapping(value = "/list.page")
    public ModelAndView list() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<PermissionBean> permissionBeanList = SpringSecurityUtils.getCurrentUser().getList();
        List<Map> operaButton = new ArrayList<>();
        for (PermissionBean bean : permissionBeanList) {
            if (bean.getPermission_url().equals("/pmOrder/detail.page")) {
                Map map = new HashMap();
                map.put("name", bean.getPermission_name());
                map.put("fn", "detail");
                operaButton.add(map);
            }
            if (bean.getPermission_url().equals("/pmOrder/modify.json")) {
                Map map = new HashMap();
                map.put("name", bean.getPermission_name());
                map.put("fn", "modify");
                operaButton.add(map);
            }
        }
        mv.addObject("operaButton", operaButton);
        mv.setViewName("/shopping/pmOrderList");
        return mv;
    }

    @RequestMapping("/list.json")
    public JsonData getList(String key, String page, String limit) {
        if (StringUtils.isBlank(page)) {
            page = StaticUtil.pageIndex;
        }
        if (StringUtils.isBlank(limit)) {
            limit = StaticUtil.sizeCount;
        }
        List<PMOrderBean> list = pmOrderService.findAllList(key, page, limit);
        if (list.size() > 0) {
            StringBuffer bf = new StringBuffer();
            for (PMOrderBean bean : list) {
                bf.append("'").append(bean.getOrder_code()).append("',");
            }
            Map map = new HashMap();
            String value = bf.substring(0, bf.length() - 1);
            map.put("type", "25");
            map.put("surface_id", value);
            try {
                List<String> arr = OrderIOC.pmOrderIsExist(StringUtil.ObjectToJson(map));
                for (PMOrderBean pmOrderBean : list) {
                    pmOrderBean.setOrder_date(DateUtil.formatDate(pmOrderBean.getOrder_date()));
                    if (arr.size() > 0) {
                        for (int j = 0; j < arr.size(); j++) {
                            if (arr.get(j).equals(pmOrderBean.getOrder_code())) {
                                pmOrderBean.setOrder_status("11");//如果订单中心有返回该条订单，则标记已导入
                                break;
                            } else {
                                pmOrderBean.setOrder_status("99");//99表示未导入
                            }
                        }
                    } else {
                        pmOrderBean.setOrder_status("99");
                    }
                }
            } catch (Exception e) {
                log.error("列表请求失败：" + e.toString());
                return JsonData.fail("请求失败，请稍后重试");
            }


        }
        int count = pmOrderService.getAllListCount(key);
        Map resultMap = new HashMap();
        resultMap.put("list", list);
        resultMap.put("total", count);
        return JsonData.success(resultMap);
    }

    @RequestMapping("/update.json")
    public JsonData update(String order_id) {
        List<PMOrderBean> list = pmOrderService.findAllByOrderId(order_id);
        if (list.size() > 0) {
            PMOrderBean pmOrderBean = list.get(0);
            pmOrderBean.setOrder_cancel_status("99");
            pmOrderService.save(pmOrderBean);
            return JsonData.success();
        } else {
            return JsonData.fail();
        }
    }

    /**
     * pm采购订单数据
     *
     * @return
     */
    @Transactional
    @RequestMapping(value = "/getOrderListByPM.json")
    public JsonData getOrderList() throws IOException {
        /**
         * 流程状态
         * * COMPLETED 已结束
         * WAITSIGN  待审批
         * PENDING 审批中
         * 如果workflow 为null 则为草稿
         * BACK 已驳回
         */
        JsonData requestResu = AirSoftService.getOrderList(StaticUtil.environmentEnum);
//        String requestResu = HttpUtil.getDemo(StaticUtil.changeCenter + "/airSoft/orderList.json");
        if (requestResu == null) {
            return JsonData.fail("PM系统请求失败，请稍后重试");
        }
//        Map map = StringUtil.JsonToMap(requestResu);
        if (requestResu.isRet()) {
//        if (map.get("ret").equals(true)) {
            StringBuffer bf = new StringBuffer();
            Map pmData = StringUtil.JsonToMap(requestResu.getData().toString());
            JSONArray json = JSONArray.fromObject(pmData.get("rows"));
            try {
                for (int i = 0; i < json.size(); i++) {
                    Map abc = StringUtil.JsonToMap(json.getString(i));
                    PMOrderBean pmOrderBean = new PMOrderBean();
                    pmOrderBean.setOrder_id(abc.get("id").toString());
                    pmOrderBean.setOrder_code(abc.get("code").toString());
                    if (abc.get("purchaseOrderName") != null) {
                        pmOrderBean.setOrder_name(abc.get("purchaseOrderName").toString());
                    } else {
                        pmOrderBean.setOrder_name("");
                    }
                    if (abc.get("workflow") != null) {
                        Map workflow = StringUtil.JsonToMap(StringUtil.ObjectToJson(abc.get("workflow")));
                        pmOrderBean.setWorkflow_status(workflow.get("status").toString());
                    } else {
                        pmOrderBean.setWorkflow_status("");
                    }
                    if (abc.get("auditor") != null) {
                        pmOrderBean.setOrder_auditor("true");
                    } else {
                        pmOrderBean.setOrder_auditor("false");
                    }
                    pmOrderBean.setOrder_date(DateUtil.timeStampToDate(abc.get("orderDate").toString()));
                    pmOrderBean.setOrder_amount(abc.get("orderAmount").toString());
                    if (abc.get("project") != null) {
                        Map project = StringUtil.JsonToMap(StringUtil.ObjectToJson(abc.get("project")));
                        pmOrderBean.setProject_name(project.get("name").toString());
                    } else {
                        pmOrderBean.setProject_name("");
                    }
                    if (abc.get("supplier") != null) {
                        Map supplier = StringUtil.JsonToMap(StringUtil.ObjectToJson(abc.get("supplier")));
                        pmOrderBean.setSupplier_name(supplier.get("name").toString());
                    } else {
                        pmOrderBean.setSupplier_name("");
                    }
                    if (abc.get("buyer") != null) {
                        Map buyer = StringUtil.JsonToMap(StringUtil.ObjectToJson(abc.get("buyer")));
                        pmOrderBean.setBuyer_name(buyer.get("realname").toString());
                    } else {
                        pmOrderBean.setBuyer_name("");
                    }
                    if (abc.get("deliveryAddress") != null) {
                        pmOrderBean.setConsignee_address(abc.get("deliveryAddress").toString());
                    } else {
                        pmOrderBean.setConsignee_address("");
                    }
                    if (abc.get("supplierContact") != null) {
                        pmOrderBean.setSupplierContact(abc.get("supplierContact").toString());
                    } else {
                        pmOrderBean.setSupplierContact("");
                    }
//                    if (abc.get("organizationName") != null) {
//                        pmOrderBean.setOrganizationName(abc.get("organizationName").toString());
//                    } else {
                    pmOrderBean.setOrganizationName("上海集栋装饰工程有限公司");
//                    }
                    if (abc.get("receiver") != null) {
                        Map receiver = StringUtil.JsonToMap(StringUtil.ObjectToJson(abc.get("receiver")));
                        pmOrderBean.setConsignee_information(receiver.get("realname").toString());
                    } else {
                        pmOrderBean.setConsignee_information("");
                    }
                    if (abc.get("comment") != null) {
                        pmOrderBean.setRemark(abc.get("comment").toString());
                    } else {
                        pmOrderBean.setRemark("");
                    }
                    if (StringUtil.result_date(pmOrderBean.getOrder_date(), "2019-01-01")) {
                        PMOrderBean pmOrderBean1 = new PMOrderBean();
                        pmOrderBean1.setOrder_code(pmOrderBean.getOrder_code());
                        Boolean bool = pmOrderService.isExisit(pmOrderBean1);
                        if (!bool) {
                            pmOrderService.save(pmOrderBean);
                        }
                    }
                }
            } catch (Exception e) {
                return JsonData.fail(e.toString());
            }
            return JsonData.success();
        } else {
//            return JsonData.fail("PM系统请求异常");
            return requestResu;
        }
    }

    @RequestMapping(value = "/submitOrderByPM.json", method = RequestMethod.POST)
    public JsonData submitOrderByPM(String dataId) throws Exception {
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        ContractBean checkBean = new ContractBean();
        checkBean.setContract_states("11");
        String company_id = userDetail.getCompany_id();
        checkBean.setCompanyB_id("20");
//        if (company_id.equals("29") || company_id.equals("31") || company_id.equals("32") || company_id.equals("33") || company_id.equals("34") || company_id.equals("35")) {
//            checkBean.setCompanyB_id("20");
//        } else {
//            checkBean.setCompanyB_id(checkBean.getCompanyB_id());
//        }
        List<ContractBean> contractBeanList = contractService.findAll(checkBean);
        if (contractBeanList.size() == 0) {
            return JsonData.fail("合同过期。");
        }
        ContractBean contractBean = contractBeanList.get(0);
        List<PMOrderBean> pmOrderBeans = pmOrderService.findAllByOrderId(dataId);
        List<Map> listMap = SkuProcess(dataId, pmOrderBeans.get(0).getOrder_code());
        if (listMap == null) {
            return JsonData.fail("无法匹配SKU数据,无法导入！");
        }
        for (int i = 0; i < listMap.size(); i++) {
            if (!"00".equals(listMap.get(i).get("status").toString())|| StringUtils.isBlank(String.valueOf(listMap.get(i).get("arriveTime")))) {
                return JsonData.fail("无法匹配SKU数据,无法导入！");
            }
        }
        AddreessBean addreessBean = new AddreessBean();
        // 当前登陆用户企业信息
        Optional<CompanyBean> optional = companyService.findById(Integer.valueOf(company_id));
        // 合同甲方企业信息
        Optional<CompanyBean> optionalA = companyService.findById(Integer.valueOf(contractBean.getCompanyA_id()));
        addreessBean.setOrder_source("PM");
        addreessBean.setConsignee_name(userDetail.getUsername());
        Map buyer = userService.findUserIdForPort(pmOrderBeans.get(0).getBuyer_name(), "20");
        if (buyer.size() == 0) {
//            addreessBean.setUser_id(0);
//            return JsonData.fail("无法匹配客户联系人，无法导入");
        } else {
            addreessBean.setUser_id(Integer.valueOf(buyer.get("id").toString()));
        }
        if (StringUtils.isBlank(pmOrderBeans.get(0).getConsignee_information())) {
            return JsonData.fail("收货人信息未填，无法导入");
        }
        Map Consignee_information = userService.findUserIdForPort(pmOrderBeans.get(0).getConsignee_information(), "20");
        if (StringUtils.isBlank(String.valueOf(Consignee_information.get("id")))) {
            return JsonData.fail("无法匹配收货人，无法导入");
        }
        if (StringUtils.isBlank(pmOrderBeans.get(0).getProject_name())) {
            return JsonData.fail("项目信息未填，无法导入");
        }
        if (StringUtils.isBlank(pmOrderBeans.get(0).getSupplierContact())) {
            return JsonData.fail("供应商联系人信息未填，无法导入");
        }
        if (StringUtils.isBlank(pmOrderBeans.get(0).getConsignee_address())) {
            return JsonData.fail("收货地址信息未填，无法导入");
        }
        addreessBean.setOrder_name(pmOrderBeans.get(0).getOrder_name());
        addreessBean.setProject_name(pmOrderBeans.get(0).getProject_name());
        addreessBean.setConsignee_information(pmOrderBeans.get(0).getConsignee_information());
        addreessBean.setConsignee_address(pmOrderBeans.get(0).getConsignee_address());
        addreessBean.setSurface_id(pmOrderBeans.get(0).getOrder_code());
        addreessBean.setOrderBill_id(pmOrderBeans.get(0).getOrder_code());
        BigDecimal price = new BigDecimal("0");
        for (Map map : listMap) {
            BigDecimal purNum = new BigDecimal(map.get("purNum").toString());
            BigDecimal sku_contract_price = new BigDecimal(map.get("sku_contract_price").toString());
            BigDecimal num = purNum.multiply(sku_contract_price);
            price = price.add(num);
        }
        addreessBean.setPrice(price.setScale(2, BigDecimal.ROUND_HALF_UP).toString());
        addreessBean.setRemark(pmOrderBeans.get(0).getRemark());
        addreessBean.setType("analog");
        addreessBean.setPartyA_id(String.valueOf(optionalA.get().getId()));
        addreessBean.setPartyA_name(optionalA.get().getCompany_name());
        addreessBean.setCrtTime(DateUtil.formatDate(pmOrderBeans.get(0).getOrder_date()));
        addreessBean.setContext(StringUtil.ObjectToJson(listMap));
        addreessBean.setUserGroup_id(20);
        addreessBean.setBuyer(pmOrderBeans.get(0).getSupplierContact());
        addreessBean.setUserGroup_name(contractBean.getPartyB());
        addreessBean.setId(0);// 默认0，方便熊数据解析，没有实际用途
        addreessBean.setDiscount("0");// 优惠金额默认为0
        addreessBean.setTransport_price("0");// 运费默认为0
        String param = StringUtil.ObjectToJson(addreessBean);
        try {
            com.ordercore.Bean.OrderBean orderBean = OrderIOC.baseAnalysisBill(param);
            //导入成功消息推送
            List<UserBean> userList = userService.findAllUserByGroupId(StaticUtil.GONGYINGLIANGROUP);
            StringBuffer buffer = new StringBuffer();
            for (UserBean userBean : userList) {
                buffer.append(userBean.getId()).append(",");
            }
            //导入成功后同步到金蝶
            Map map = new HashMap();
            map.put("orderBill_id", orderBean.getOrderBill_id());
            map.put("orderBean_pk", orderBean.getOrderBean_pk());
            map.put("orderBill_flow", "25");
            map.put("orderBill_customStat", "1002");
            String param1 = StringUtil.ObjectToJson(map);
            try {
                com.ordercore.Bean.OrderBean orderBean1 = OrderIOC.updateOrder(param1);
                Map map1 = new HashMap<>();
                map1.put("order_id", orderBean.getOrderBill_id());
                map1.put("orderBean_pk", orderBean.getOrderBean_pk());
                map1.put("type", "25");
                Map result = OrderIOC.queryDeteilById(StringUtil.ObjectToJson(map1));
                messageService.rabbitMq("kingDeeOrderChange", "kingDeeOrderKey", StringUtil.ObjectToJson(result));
                MessageBean messageBean = new MessageBean();
                messageBean.setMessage_creater("1");
//                    messageBean1.setMessage_creater(SpringSecurityUtils.getCurrentUser().getId().toString());
                messageBean.setMessage_no(orderBean.getOrderBill_id());
                messageBean.setMessage_url("/order/orderDetail.page?orderNo=" + orderBean.getOrderBill_id().toString() + "orderBean_pk=" + orderBean.getOrderBean_pk());
                messageBean.setMessage_body("编号为  " + orderBean.getOrderBill_id().toString() + "  的PM订单已生成，请及时查看并安排出库。");
//                    messageBean1.setMessage_body(param);
                messageBean.setMessage_sendee(String.valueOf(SpringSecurityUtils.getCurrentUser().getId()));
                messageBean.setMessage_status("99");
                messageBean.setMessage_type("msg001");
                messageService.send(messageBean);
                return JsonData.success();
            } catch (Exception e) {
                log.error("同步失败：" + e.toString());
                return JsonData.fail("同步失败，请稍后重试！");
            }
        } catch (Exception e) {
            log.error("同步失败：" + e.toString());
            return JsonData.fail("同步失败，请稍后重试！");
        }
    }

    @RequestMapping(value = "/detailOrderByPM.page")
    public ModelAndView detailOrderPMPage(String dataId) {
        ModelAndView mv = new ModelAndView();
        List<PMOrderBean> pmOrderBeans = pmOrderService.findAllByOrderId(dataId);
//        List<Map> skuList = SkuProcess(dataId, pmOrderBeans.get(0).getOrder_code());
//        mv.addObject("skuList", StringUtil.ObjectToJson(skuList));
        mv.addObject("dataId", dataId);
        mv.addObject("surface_id", pmOrderBeans.get(0).getOrder_code());
        mv.addObject("project_name", pmOrderBeans.get(0).getProject_name());
        mv.addObject("buyer", pmOrderBeans.get(0).getBuyer_name());
        mv.addObject("supplierContact", pmOrderBeans.get(0).getSupplierContact());
        mv.addObject("organizationName", "上海集栋装饰工程有限公司");
        mv.addObject("consignee_information", pmOrderBeans.get(0).getConsignee_information());
        mv.addObject("consignee_address", pmOrderBeans.get(0).getConsignee_address());
        mv.addObject("remark", pmOrderBeans.get(0).getRemark());
        mv.addObject("crtTime", DateUtil.formatDate(pmOrderBeans.get(0).getOrder_date()));
        mv.setViewName("/shopping/pmOrderDetail");
        return mv;
    }

    @RequestMapping(value = "/detailOrderSkuListByPM.json")
    public JsonData detailOrderSkuListByPM(String dataId, String order_code) {
        return JsonData.success(SkuProcess(dataId, order_code));
    }

    private List<Map> SkuProcess(String dataId, String surface_id) {
        ContractBean checkBean = new ContractBean();
        checkBean.setContract_states("11");
        checkBean.setCompanyB_id("20");
        checkBean.setCompanyB_id(checkBean.getCompanyB_id());
        List<ContractBean> contractBeanList = contractService.findAll(checkBean);
        if (contractBeanList.size() == 0) {
            return null;
        } else {
            ContractBean contractBean = contractBeanList.get(0);
            List<Map> listMap = new ArrayList<>();
            JsonData result = AirSoftService.getOrderBody(dataId, StaticUtil.environmentEnum);
            if (result == null) {
                return null;
            }
            if (result.isRet()) {
                StringBuffer bf = new StringBuffer();
                Map pmData = StringUtil.JsonToMap(result.getData().toString());
                JSONArray json = JSONArray.fromObject(pmData.get("rows"));
                if (json.size() > 0) {
                    for (int i = 0; i < json.size(); i++) {
                        Map Sku = StringUtil.JsonToMap(json.getString(i));
                        Map abc = StringUtil.JsonToMap(StringUtil.ObjectToJson(Sku.get("material")));
                        if (StringUtils.isBlank(abc.get("code").toString()) || abc.get("code") == null) {
                            return null;
                        } else {
                            List<ContractRealTimePriceBean> skuList = contractRealTimePriceService.getSkuBySkuCodeAndContractIdDetailOfPm(abc.get("code").toString(),
                                    contractBean.getId().toString());
                            if (skuList.size() > 0) {
                                ContractRealTimePriceBean contractRealTimePriceBean = skuList.get(0);
                                Map skuBean = new HashMap();
                                skuBean.put("surface_id", surface_id);
                                skuBean.put("sku_cost_price", contractRealTimePriceBean.getSku_cost_price());
                                skuBean.put("sku_official_price", contractRealTimePriceBean.getSku_official_price());
                                skuBean.put("sku_contract_price", contractRealTimePriceBean.getSku_contract_price());
                                skuBean.put("sku_slug_set", contractRealTimePriceBean.getSku_slug_set());
                                skuBean.put("sku_pic_url", contractRealTimePriceBean.getSku_pic_url());
                                skuBean.put("sku_name", contractRealTimePriceBean.getSku_name());
                                skuBean.put("sku_unit", contractRealTimePriceBean.getSku_unit());
                                skuBean.put("sku_brand", contractRealTimePriceBean.getSku_brand());
                                skuBean.put("sku_code", contractRealTimePriceBean.getSku_code());
                                skuBean.put("sku_model", contractRealTimePriceBean.getSku_model());
                                if (Sku.get("planArrivalDate") == null) {
                                    skuBean.put("arriveTime", "");
                                } else {
                                    skuBean.put("arriveTime",
                                            DateUtil.timeStampToDate(Sku.get("planArrivalDate").toString()));
                                }
                                skuBean.put("purNum", Sku.get("unInvoiceNum"));
                                skuBean.put("sku_remark", Sku.get("comment"));
                                skuBean.put("status", contractRealTimePriceBean.getStatus());
                                listMap.add(skuBean);
                            } else {
                                ResponseData responseData = NonStandardLibraryIOC.getNonStandardLibraryDetail(abc.get("code").toString());
                                if (responseData.isRet()) {
                                    NonStandardLibraryBean requestNonStandardLibraryBean = (NonStandardLibraryBean) responseData.getData();
                                    Map skuBean = new HashMap();
                                    skuBean.put("surface_id", surface_id);
                                    skuBean.put("sku_cost_price", requestNonStandardLibraryBean.getSku_price());
                                    skuBean.put("sku_official_price", requestNonStandardLibraryBean.getSku_price());
                                    skuBean.put("sku_contract_price", requestNonStandardLibraryBean.getSku_price());
                                    skuBean.put("sku_slug_set", requestNonStandardLibraryBean.getSku_slug_set());
                                    skuBean.put("sku_pic_url", "无");
                                    skuBean.put("sku_name", requestNonStandardLibraryBean.getSku_name());
                                    skuBean.put("sku_unit", requestNonStandardLibraryBean.getSku_unit());
                                    skuBean.put("sku_brand", requestNonStandardLibraryBean.getSku_brand());
                                    skuBean.put("sku_code", requestNonStandardLibraryBean.getSku_code());
                                    if (Sku.get("planArrivalDate") == null) {
                                        skuBean.put("arriveTime", "");
                                    } else {
                                        skuBean.put("arriveTime",
                                                DateUtil.timeStampToDate(Sku.get("planArrivalDate").toString()));
                                    }
                                    skuBean.put("purNum", Sku.get("unInvoiceNum"));
                                    skuBean.put("status", requestNonStandardLibraryBean.getSku_status());
                                    listMap.add(skuBean);
                                } else {
                                    Map skuBean = new HashMap();
                                    if (Sku.get("planArrivalDate") == null) {
                                        skuBean.put("arriveTime", "");
                                    } else {
                                        skuBean.put("arriveTime",
                                                DateUtil.timeStampToDate(Sku.get("planArrivalDate").toString()));
                                    }
                                    skuBean.put("sku_contract_price", 0);
                                    skuBean.put("purNum", Sku.get("unInvoiceNum"));
                                    skuBean.put("sku_code", abc.get("code").toString());
                                    skuBean.put("surface_id", surface_id);
                                    skuBean.put("status", 33);
                                    listMap.add(skuBean);
                                }
                            }
                        }
                    }
                    return listMap;
                } else {
                    return null;
                }
            } else {
                return null;
            }
        }
    }

    @RequestMapping("/refreshPMOrderStatus.json")
    public JsonData refreshPMOrderStatus(String order_id) {
        return pmOrderService.refreshPMOrderStatus(order_id);
    }

    @RequestMapping(value = "/refuse.json", method = RequestMethod.POST)
    public JsonData refuse(String reason) throws Exception {
        Map map = StringUtil.JsonToMap(reason);
        MessageBean messageBean = new MessageBean();
        messageBean.setMessage_creater(String.valueOf(SpringSecurityUtils.getCurrentUser().getId()));
//                    messageBean1.setMessage_creater(SpringSecurityUtils.getCurrentUser().getId().toString());
        messageBean.setMessage_no(map.get("order_code").toString());
        messageBean.setMessage_url("/pmOrder/detailOrderByPM.page?dataId=" + map.get("order_id").toString());
        messageBean.setMessage_body(map.get("reason").toString());
//                    messageBean1.setMessage_body(param);
        if (StringUtils.isBlank(String.valueOf(map.get("buyer_name")))) {
            messageBean.setMessage_sendee("1");
        } else {
            Map buyer = userService.findUserIdForPort(map.get("buyer_name").toString(), "20");
            if (StringUtils.isNotBlank(String.valueOf(buyer.get("id")))) {
                messageBean.setMessage_sendee(String.valueOf(buyer.get("id")));
            } else {
                messageBean.setMessage_sendee("1");
            }
        }
        messageBean.setMessage_status("99");
        messageBean.setMessage_type("msg001");
        messageService.send(messageBean);
        return JsonData.success();
    }
}
